extends Control


func _on_btn_3_pressed() -> void:
	get_tree().quit()

func _on_btn_1_pressed() -> void:
	$Init.visible = true

func _ready() -> void:
	print(InitRoleData())
	
	var file = FileAccess.open("res://game/save/roles.json", FileAccess.READ)
	var content = file.get_as_text()
	var result = JSON.parse_string(content)
	
	# 初始化值
	var newRole = {
		"Team": 0,
		"FaceTowards": 0,
		"Dead": 0,
		"Step": 0,
		"ExpGot": 0,
		"Auto": 0,
		"FightFrame": [],
		"FightingFrame": 0,
		"Moved": 0,
		"Acted": 0,
		"ActTeam": 0,
		"ShowString": '',
		"role_magic_count": 10,
		"magicID": [],
		"position_layer": {},
		"AI_MoveX": null,
		"AI_MoveY": null,
		"AI_ActionX": null,
		"AI_ActionY": null,
		"AI_Magic": null,
		"AI_Item": null,
		"prevX": null,
		"prevY": null,
		"AI_Action": 0,
		"dead_alpha": 255,
		"data":result[0],
	}
	
	var roleData = newRole["data"]
	
	box1.find_child('Name').text = str(roleData.Name)
	box1.find_child('HP').text = str(int(roleData.HP))
	box1.find_child('MaxHP').text = str(int(roleData.MaxHP))
	box1.find_child('Level').text = str(int(roleData.Level))
	box1.find_child('Exp').text = str(int(roleData.Exp))
	box1.find_child('exp_up').text = str(1)
	box1.find_child('PhysicalPower').text = str(int(roleData.PhysicalPower))
	
	
	var color
	if roleData.MPType == 0:
		color = Color8(208, 152, 208, 255)
	else:
		color = Color8(236, 200, 40, 255)
	box1.find_child('MaxMP').set("theme_override_colors/font_color", color)
	box1.find_child('MP').set("theme_override_colors/font_color", color)
	
	box1.find_child('MaxMP').text = str(int(roleData.MaxMP))
	box1.find_child('MP').text = str(int(roleData.MP))
	
	box1.find_child('Attack').text = str(int(roleData.Attack))
	box1.find_child('Defence').text = str(int(roleData.Defence))
	box1.find_child('Speed').text = str(int(roleData.Speed))
	box1.find_child('Medcine').text = str(int(roleData.Medcine))
	box1.find_child('Detoxification').text = str(int(roleData.Detoxification))
	box1.find_child('UsePoison').text = str(int(roleData.UsePoison))
	
	box1.find_child('Fist').text = str(int(roleData.Fist))
	box1.find_child('Sword').text = str(int(roleData.Sword))
	box1.find_child('Knife').text = str(int(roleData.Knife))
	box1.find_child('Unusual').text = str(int(roleData.Unusual))
	box1.find_child('HiddenWeapon').text = str(int(roleData.HiddenWeapon))
	
	# TODO 武学这个数据
	# var magic = this.j.save.getRoleLearnedMagic(role, i);
	file = FileAccess.open("res://game/save/magics.json", FileAccess.READ)
	content = file.get_as_text()
	var magics = JSON.parse_string(content)
	
	# 如果是0就就不显示
	# MagicLevel 武学 + 武功等级
	#print(magics[1].data)
	#print(roleData.MagicID)
	
	file = FileAccess.open("res://game/save/savedata.json", FileAccess.READ)
	content = file.get_as_text()
	var savedata = JSON.parse_string(content)
	# 角色初始化场景坐标
	print(savedata.MainMapX)
	print(savedata.MainMapY)
	
	# this.readData(0, this.resources, this.savedata, length_arr, offset);
	# this.readData(1, this.resources, this.roles, length_arr, offset);
	
	#c.TitleScene.prototype.op1 = function() {    地图显示
	#c.SubScene.prototype.start = function(towards) { 子场景显示


func InitRoleData() -> Dictionary:
	var roleData = {}
	
	# 1、最大生命值(25~50)
	roleData["MaxHP"] = 25 + rand(26)
	# 2、当前生命值
	roleData["HP"] = roleData["MaxHP"]
	# 3、最大魔法值(25~50)
	roleData["MaxMP"] = 25 + rand(26)
	# 4、当前魔法值
	roleData["MP"] = roleData["MaxMP"]
	# 5、魔法类型(0~1)
	roleData["MPType"] = rand(2)
	# 6、生命恢复(1~10)
	roleData["IncLife"] = 1 + rand(10)
	# 7、攻击力(25~30)
	roleData["Attack"] = 25 + rand(6)
	# 8、速度(25~30)
	roleData["Speed"] = 25 + rand(6)
	# 9、防御力(25~30)
	roleData["Defence"] = 25 + rand(6)
	# 10、药品使用(25~30)
	roleData["Medcine"] = 25 + rand(6)
	# 11、使用毒药(25~30)
	roleData["UsePoison"] = 25 + rand(6)
	# 12、解毒能力(25~30)
	roleData["Detoxification"] = 25 + rand(6)
	# 13、拳法(25~30)
	roleData["Fist"] = 25 + rand(6)
	# 14、剑法(25~30)
	roleData["Sword"] = 25 + rand(6)
	# 15、刀法(25~30)
	roleData["Knife"] = 25 + rand(6)
	# 16、特殊技能(25~30)
	roleData["Unusual"] = 25 + rand(6)
	# 17、暗器(25~30)
	roleData["HiddenWeapon"] = 25 + rand(6)
	# 18、智力(25~125)
	roleData["IQ"] = 25 + rand(100)
	return roleData

func rand(i: int) -> int:
	return randi_range(0, i-1)

func _on_button_pressed() -> void:
	$Init.visible = false


func _on_button_3_pressed() -> void:
	print("进入游戏")

@onready var box1 = $Init/Box1

func _on_button_2_pressed() -> void:
	var roleData = InitRoleData()
	
	box1.find_child('HP').text = str(roleData.HP)
	box1.find_child('MaxHP').text = str(roleData.MaxHP)
	
	var color
	if roleData.MPType == 0:
		color = Color8(208, 152, 208, 255)
	else:
		color = Color8(236, 200, 40, 255)
	box1.find_child('MaxMP').set("theme_override_colors/font_color", color)
	box1.find_child('MP').set("theme_override_colors/font_color", color)
	
	box1.find_child('MaxMP').text = str(roleData.MaxMP)
	box1.find_child('MP').text = str(roleData.MP)
	
	box1.find_child('Attack').text = str(roleData.Attack)
	box1.find_child('Defence').text = str(roleData.Defence)
	box1.find_child('Speed').text = str(roleData.Speed)
	box1.find_child('Medcine').text = str(roleData.Medcine)
	box1.find_child('Detoxification').text = str(roleData.Detoxification)
	box1.find_child('UsePoison').text = str(roleData.UsePoison)
	
	box1.find_child('Fist').text = str(roleData.Fist)
	box1.find_child('Sword').text = str(roleData.Sword)
	box1.find_child('Knife').text = str(roleData.Knife)
	box1.find_child('Unusual').text = str(roleData.Unusual)
	box1.find_child('HiddenWeapon').text = str(roleData.HiddenWeapon)
